app.controller('AppCreateInTabCtrl', ['$rootScope', '$scope', 'httpLoad', '$modal', '$stateParams', '$state', function ($rootScope, $scope, httpLoad, $modal, $stateParams, $state) {
    $rootScope.moduleTitle = '应用管理 > 应用构建';
    $rootScope.link = '/statics/css/tpl-card.css';
    $scope.isBack = false;
    $rootScope.taskID=$stateParams.taskId;
    $rootScope.firstLi=true;
    $rootScope.secondLi=true;
    $rootScope.threeLi=true;
    $rootScope.threeLi_war=false;
    $rootScope.fourLi=true;
    $rootScope.fourLi_img=false;
    $rootScope.fiveLi=true;
    $rootScope.addLi=false;
    var flag = $stateParams.flag;
    var flag_app = sessionStorage.getItem('appCreateInTabLocation')||1;
    switch (flag_app/1){
        case 1:
            $scope.isappLiu = true;
            break;
        case 2:
            $scope.isAppLog = true;
            break;
        case 3:
            $scope.isAnalysis = true;
            break;
    }
    sessionStorage.setItem('appCreateInTabLocation','1');
    $scope.goBack = function(){
        if(flag==0){
            $state.go('app.application.create');
        }
        if(flag==1){
            $state.go('app.application.develop');
        }
    };
}]);
app.controller('appLiuCtrl', ['$rootScope', '$scope', 'httpLoad', '$modal', '$stateParams', '$state',function ($rootScope, $scope, httpLoad, $modal, $stateParams, $state) {


    $scope.appName=$stateParams.name;
    $scope.warehouseState="等待执行";
    $scope.analyseState="等待执行";
    $scope.warState="等待执行";
    $scope.warState_fu="等待执行";
    $scope.makeState="等待执行";
    $scope.makeState_fu="等待执行";
    $scope.safeState="等待执行";

    $scope.change = function(x){
        if(x==0){
            $rootScope.firstLi=true;
            $rootScope.secondLi=true;
            $rootScope.threeLi=true;
            $rootScope.threeLi_war=false;
            $rootScope.fourLi=true;
            $rootScope.fourLi_img=false;
            $rootScope.fiveLi=true;
            $rootScope.addLi=false;
        }
        if(x==1){
            $rootScope.firstLi=false;
            $rootScope.secondLi=false;
            $rootScope.threeLi=false;
            $rootScope.threeLi_war=true;
            $rootScope.fourLi=true;
            $rootScope.fourLi_img=false;
            $rootScope.fiveLi=true;
            $rootScope.addLi=false;
        }
        if(x==2){
            $rootScope.firstLi=false;
            $rootScope.secondLi=false;
            $rootScope.threeLi=false;
            $rootScope.threeLi_war=false;
            $rootScope.fourLi=false;
            $rootScope.fourLi_img=true;
            $rootScope.fiveLi=true;
            $rootScope.addLi=false;
        }
    }

    //编辑
    $scope.updateFirst = function(){
        var modalInstance = $modal.open({
            templateUrl : '/statics/tpl/application/create/updateFirst.html',
            controller : 'updateFirstModalCtrl',// 初始化模态范围
        });
        modalInstance.result.then(function(data){
            $scope.wareHouseType=data.data.repository_type;
            $scope.addr=data.data.repositoryUrl;
            $scope.fenZhi="master";
        },function(){});
    }
    $scope.updateThree = function(){
        var modalInstance = $modal.open({
            templateUrl : '/statics/tpl/application/create/updateThree.html',
            controller : 'updateThreeModalCtrl',// 初始化模态范围
        });
        modalInstance.result.then(function(){
        },function(){});
    }
    $scope.updateFour = function(){
        var modalInstance = $modal.open({
            templateUrl : '/statics/tpl/application/create/updateFour.html',
            controller : 'updateFourModalCtrl',// 初始化模态范围
        });
        modalInstance.result.then(function(){
        },function(){});
    }

    //启动
    $scope.start = function($event,value){
        /*var host = window.location.host;
        // socket = new WebSocket('ws://' + host + '/messagingService');
        socket = new WebSocket('ws://192.168.5.148:8090/devops-pipeline/messageService');
        socket.onopen = function() {
        };
        socket.onclose = function() {
        };
        socket.onmessage = function(event) {
            var obj = JSON.parse(event.data);
            message = JSON.parse(obj.content);
        }*/
        // $($event.target).hide();
        // $($event.target).next().show();
        switch (value){
            case 0:
                $scope.warehouseState="执行中";
                $scope.analyseState="执行中";
                $scope.warState="执行中";
                $scope.makeState="执行中";
                $scope.safeState="执行中";
                break;
            case 1:
                $scope.warehouseState="执行中";
                break;
            case 2:
                $scope.analyseState="执行中";
                break;
            case 3:
                $scope.warState="执行中";
                break;
            case 4:
                $scope.warState_fu="执行中";
                break;
            case 5:
                $scope.makeState="执行中";
                break;
            case 6:
                $scope.makeState_fu="执行中";
                break;
            case 7:
                $scope.safeState="执行中";
                break;
        }
        httpLoad.loadData({
            url:'/pipeline/pipeline/startTask',
            data: {taskId:$rootScope.taskID,
                taskStage:value,
                envId:1,
                registryUrl:"abcsys.cn:5000"},
            success:function(data){
                if(data.success){
                    switch (value){
                        case 0:
                            $scope.warehouseState="执行成功";
                            $scope.analyseState="执行成功";
                            $scope.warState="执行成功";
                            $scope.makeState="执行成功";
                            $scope.safeState="执行成功";
                            break;
                        case 1:
                            $scope.warehouseState="执行成功";
                            break;
                        case 2:
                            $scope.analyseState="执行成功";
                            break;
                        case 3:
                            $scope.warState="执行成功";
                            break;
                        case 4:
                            $scope.warState_fu="执行成功";
                            break;
                        case 5:
                            $scope.makeState="执行成功";
                            break;
                        case 6:
                            $scope.makeState_fu="执行成功";
                            break;
                        case 7:
                            $scope.safeState="执行成功";
                            break;
                    }
                    $scope.pop(data.message);
                    if(data.data.newImage==1){
                        var param = data.data.imageModel;
                        httpLoad.loadData({
                            url:'/runtime/image/create',
                            data: param,
                            success:function(data){
                                if(data.success){
                                    $scope.pop(data.message);
                                }else{
                                    $scope.pop(data.message,"error");
                                }
                            }
                        });
                    }
                }else{
                    switch (value){
                        case 0:
                            $scope.warehouseState="执行失败";
                            $scope.analyseState="执行失败";
                            $scope.warState="执行失败";
                            $scope.makeState="执行失败";
                            $scope.safeState="执行失败";
                            break;
                        case 1:
                            $scope.warehouseState="执行失败";
                            break;
                        case 2:
                            $scope.analyseState="执行失败";
                            break;
                        case 3:
                            $scope.warState="执行失败";
                            break;
                        case 4:
                            $scope.warState_fu="执行失败";
                            break;
                        case 5:
                            $scope.makeState="执行失败";
                            break;
                        case 6:
                            $scope.makeState_fu="执行失败";
                            break;
                        case 7:
                            $scope.safeState="执行失败";
                            break;
                    }
                    $scope.pop(data.message,'error');
                }
            }
        });
    }

    //新增子项目卡片
    $scope.addApp = function(){  //打开模态
        var modalInstance = $modal.open({
            templateUrl : '/statics/tpl/application/create/add.html',
            controller : 'addAppModalCtrl',// 初始化模态范围
            resolve : {
                selectId: function() {
                    return  $scope.x;
                },
                secondLi: function() {
                    return  $rootScope.secondLi;
                },
                fiveLi: function() {
                    return  $rootScope.fiveLi;
                },
            }
        });
        modalInstance.result.then(function(){
        },function(){});
    };
    $rootScope.deleteTwo = function(){  //打开模态
        $rootScope.secondLi=false;
        $rootScope.addLi=true;
        if($rootScope.taskID){
            var clairEnableval=0;
            var sonarEnableval=0;
            if($rootScope.secondLi){
                sonarEnableval=1;
            }
            if($rootScope.fiveLi){
                clairEnableval=1;
            }
            httpLoad.loadData({
                url:'/runtime/image/create',
                data: {taskId:$rootScope.taskID,
                    sonarEnableval:sonarEnableval,
                    clairEnableval:clairEnableval,
                },
                success:function(data){
                    if(data.success){
                        $scope.pop(data.message);
                    }
                }
            });
        }
    };
    $scope.deleteFive = function(){  //打开模态
        $rootScope.fiveLi=false;
        $rootScope.addLi=true;
        if($rootScope.taskID){
            var clairEnableval=0;
            var sonarEnableval=0;
            if($rootScope.secondLi){
                sonarEnableval=1;
            }
            if($rootScope.fiveLi){
                clairEnableval=1;
            }
            httpLoad.loadData({
                url:'/runtime/image/create',
                data: {taskId:$rootScope.taskID,
                    sonarEnableval:sonarEnableval,
                    clairEnableval:clairEnableval,
                },
                success:function(data){
                    if(data.success){
                        $scope.pop(data.message);
                    }
                }
            });
        }
    };
    if($rootScope.taskID!=null){
        httpLoad.loadData({
            url:'/pipeline/pipeline/getTask',
            data: {taskId:$rootScope.taskID},
            success:function(data){
                if(data.success){
                    $scope.wareHouseType = data.data.repositoryType;
                    $scope.addr = data.data.repositoryUrl;
                    $scope.fenZhi = data.data.branckSpecifier;
                    if(data.data.taskStage==1){
                        $scope.x=0;
                    }
                    if(data.data.sonarEnable==0){
                        $rootScope.secondLi=false;
                        $rootScope.addLi=true;
                    }
                    if(data.data.sonarEnable==1){
                        $rootScope.secondLi=true;
                    }
                    if(data.data.clairEnable==0){
                        $rootScope.fiveLi=false;
                        $rootScope.addLi=true;
                    }
                    if(data.data.clairEnable==1){
                        $rootScope.fiveLi=true;
                    }
                }
            }
        });
    }
    $scope.showLog = function(value){
        var modalInstance = $modal.open({
            templateUrl : '/statics/tpl/application/create/processLog.html',
            controller : 'processLogModalCtrl',
            // resolve : {
            //     id: function() {
            //         return  id;
            //     }
            // }
        });
        modalInstance.result.then(function(){
        },function(){});
    }
}])
//新增ctrl
angular.module('app').controller('addAppModalCtrl',['$rootScope','$scope','$modalInstance','LANGUAGE','httpLoad','selectId','secondLi','fiveLi',
    function($rootScope,$scope,$modalInstance,LANGUAGE,httpLoad,selectId,secondLi,fiveLi){
        if(selectId==0){
            if(secondLi==true&&fiveLi==false){
                $scope.typeData = ["镜像安全"];
            }else if(secondLi==false&&fiveLi==false){
                $scope.typeData = ["源码分析","镜像安全"];
            }else if(secondLi==false&&fiveLi==true){
                $scope.typeData = ["源码分析"];
            }
        }
        if(selectId==1){
            if(fiveLi==false){
                $scope.typeData = ["镜像安全"];
            }
        }
        if(selectId==2){
            if(fiveLi==false){
                $scope.typeData = ["镜像安全"];
            }
        }
        $scope.ok = function(){
            if(typeof($scope.addtype) == "undefined"){
                $scope.pop("请选择流程","error");
                return false;
            }
            if(selectId==0){
                if(secondLi==true&&fiveLi==false){
                    $rootScope.fiveLi = true;
                    $rootScope.addLi = false;
                }else if(secondLi==false&&fiveLi==false){
                    if($scope.addtype=="源码分析"){
                        $rootScope.secondLi=true;
                    }else{
                        $rootScope.fiveLi=true;
                    }
                    $rootScope.addLi = true;
                }else if(secondLi==false&&fiveLi==true){
                    $rootScope.secondLi = true;
                    $rootScope.addLi = false;
                }
            }
            if(selectId==1){
                if(fiveLi==false){
                    $rootScope.fiveLi = true;
                    $rootScope.addLi = false;
                }
            }
            if(selectId==2){
                if(fiveLi==false){
                    $rootScope.fiveLi = true;
                    $rootScope.addLi = false;
                }
            }
            if($rootScope.taskID){
                var clairEnableval=0;
                var sonarEnableval=0;
                if($rootScope.secondLi){
                    sonarEnableval=1;
                }
                if($rootScope.fiveLi){
                    clairEnableval=1;
                }
                httpLoad.loadData({
                    url:'/runtime/image/create',
                    data: {taskId:$rootScope.taskID,
                        sonarEnableval:sonarEnableval,
                        clairEnableval:clairEnableval,
                    },
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                        }
                    }
                });
            }
            $modalInstance.close();
        };
        $scope.cancel = function(){
            $modalInstance.dismiss('cancel'); // 退出
        };
    }]);
//编辑ctrl
angular.module('app').controller('updateFirstModalCtrl',['$rootScope','$scope','$modalInstance','LANGUAGE','httpLoad','$stateParams',
    function($rootScope,$scope,$modalInstance,LANGUAGE,httpLoad,$stateParams){
        if($rootScope.taskID!=null){
            httpLoad.loadData({
                url:'/pipeline/pipeline/getTask',
                data: {taskId:$rootScope.taskID},
                success:function(data){
                    if(data.success){
                        $scope.addData.repository_type = data.data.repositoryType;
                        $scope.addData.repositoryUrl = data.data.repositoryUrl;
                    }
                }
            });
        }
        $scope.getFen=true;
        $scope.codeFen=true;
        $scope.radioChecked = function () {
            if($scope.addData.repository_type=="git"){
                $scope.getFen=true;
                $scope.codeFen=true;
            }else if($scope.addData.repository_type=="gitLab"){
                $scope.getFen=true;
                $scope.codeFen=true;
            }else{
                $scope.getFen=false;
                $scope.codeFen=false;
            }
        }
        $scope.ok = function(){
            var clairEnableval=0;
            var sonarEnableval=0;
            if($rootScope.secondLi){
                sonarEnableval=1;
            }
            if($rootScope.fiveLi){
                clairEnableval=1;
            }
            $scope.addData={
                envId:1,
                projectName:$stateParams.name,
                userName:$rootScope.userData.userName,
                taskStage:1,
                projectId:$stateParams.id,
                taskCreator:1,
                sonarEnable:sonarEnableval,
                clairEnable:clairEnableval,
                repository_type:$scope.addData.repository_type,
                branchSpecifier:"*/master",
                repositoryUrl:$scope.addData.repositoryUrl,
                code:"src",
                registryUrl:"abcsys.cn:5000",
                registryId:1
            };
            httpLoad.loadData({
                url:'/pipeline/pipeline/createTask',
                data: $scope.addData,
                success:function(data){
                    if(data.success){
                        $rootScope.taskID = data.data.taskId;
                        $scope.pop(data.message);
                        $modalInstance.close(data);
                    }else{
                        $scope.pop(data.message,'error');
                    }
                }
            });
        };
        $scope.cancel = function(){
            $modalInstance.dismiss('cancel'); // 退出
        };
    }]);
angular.module('app').controller('updateThreeModalCtrl',['$rootScope','$scope','$modalInstance','LANGUAGE','httpLoad',
    function($rootScope,$scope,$modalInstance,LANGUAGE,httpLoad){
        $scope.cancel = function(){
            $modalInstance.dismiss('cancel'); // 退出
        };
    }]);
angular.module('app').controller('updateFourModalCtrl',['$rootScope','$scope','$modalInstance','LANGUAGE','httpLoad',
    function($rootScope,$scope,$modalInstance,LANGUAGE,httpLoad){
        $scope.cancel = function(){
            $modalInstance.dismiss('cancel'); // 退出
        };
    }]);
//流程日志ctrl
angular.module('app').controller('processLogModalCtrl',['$scope','$modalInstance','LANGUAGE','httpLoad',
    function($scope,$modalInstance,LANGUAGE,httpLoad){ //依赖于modalInstance
        httpLoad.loadData({
            url:'/pipeline/buildJobConsoleOutput',
            data:{jobName: "zhangsan_cicd_demo_20170627020230",build_number:5},
            success:function(data){
                if(data.success){
                    $scope.codeMirror.setValue(data.data);
                }else{
                    $scope.pop(data.message,'error');
                }
            }
        });
        $scope.cancel = function(){
            $modalInstance.dismiss('cancel'); // 退出
        }
    }]);
app.directive('ngCodeMirror', [function () {
    return {
        restrict: 'EA',
        scope:{
            codeMirror: '='
        },
        link: function (scope, element, attrs) {
            var editor = $(element).find('.textarea')[0];
            //初始化在线代码编辑器
            var codeMirror = CodeMirror.fromTextArea(editor, {
                theme: 'erlang-dark',
                readOnly: true
            });
            scope.codeMirror = codeMirror;
        }
    }
}]);